﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Xml;
using System.Xml.Linq;

namespace Automator.StartPage
{
    public partial class StartPageContent : UserControl
    {        
        public Hyperlink NewSolutionLink { get { return lnkNewSolution; } }
        public Hyperlink NewWorkflowLink { get { return lnkNewWorkflow; } }
        public Hyperlink OpenSolutionLink { get { return lnkOpenSolution; } }
        public Hyperlink OpenWorkflowLink { get { return lnkOpenWorkflow; } }
        public Hyperlink SqlServerLink { get { return lnkSqlServer; } }
        public Hyperlink DocumentationLink { get { return lnkDocumentation; } }
        
        private string xmlPath;
        
        public StartPageContent()
        {
            InitializeComponent();
            
            xmlPath = Path.Combine(Automator.Runtime.Environment.xmlDir, "feed.xml");
            LoadRSS();
        }
        
        /// <summary>
        /// Downloads RSS info from website
        /// </summary>
        private void DownloadRSS()
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.Proxy = Automator.Runtime.Settings.Proxy;
                    client.DownloadFile("http://automatorrss.wordpress.com/feed/", xmlPath);
                }
            }
            catch (Exception ex)
            {
                XDocument errorXml = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement("rss",
                        new XElement("channel",
                            new XElement("item",
                                new XElement("title", "Could not refresh news"),
                                new XElement("description", ex.Message)))));
                errorXml.Save(xmlPath);
            }
        }
        
        /// <summary>
        /// Binds an RSS feed to the ListBox
        /// </summary>
        private void LoadRSS()
        {
            lstRss.DataContext = null;
            XmlDataProvider provider = new XmlDataProvider();
            XmlDocument doc = new XmlDocument();

            if (File.Exists(xmlPath))
            {
                doc.Load(xmlPath);
            }
            else
            {
                XDocument emptyXml = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement("rss",
                        new XElement("channel",
                            new XElement("item",
                                new XElement("title", "Click 'Refresh News'"),
                                new XElement("description", 
                                    "Click the button to download the latest information about Automator new releases and features.")))));
                doc.Load(emptyXml.CreateReader());
            }
            provider.Document = doc;
            provider.XPath = "rss/channel";
            lstRss.DataContext = provider;
        }
        
        private void BtnRefresh_Click(object sender, RoutedEventArgs e)
        {
            btnRefresh.IsEnabled = false;
            DownloadRSS();
            LoadRSS();
            btnRefresh.IsEnabled = true;
        }
    }
}